package com.scoy.honeymei.fragment.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.home.TripDetailActivity;
import com.scoy.honeymei.activity.mall.SearchHomeActivity;
import com.scoy.honeymei.activity.mall.TripMeActivity;
import com.scoy.honeymei.adapter.TripAdapter;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.bean.TripBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ClassName: TripFragment$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/9/6/0006$ 上午 9:18$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/6/0006$ 上午 9:18$
 * @UpdateRemark: 更新内容
 */
public class TripFragment extends BaseFragment {

    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    private int type;
    private Unbinder unbinder;
    private Context mContext;
    private ArrayList<TripBean> datalist;
    private TripAdapter tripAdapter;
    private Long cTime;
    private int pageInt = 0;
    private int searchId;
    private String searchName;
    private String startplace;
    private String endplace;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            cTime = getArguments().getLong("cTime");
            startplace = getArguments().getString("startplace");
            endplace = getArguments().getString("endplace");
            searchId = getArguments().getInt("searchId");
            searchName = getArguments().getString("searchName");
        }
    }

    public static TripFragment newInstance(int type, long cTime,String startplace,String endplace) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        args.putLong("cTime", cTime);
        args.putString("startplace", startplace);
        args.putString("endplace", endplace);
        TripFragment fragment = new TripFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public static TripFragment newInstance(int searchId/*, String searchName*/) {
        Bundle args = new Bundle();
        args.putInt("searchId", searchId);
    //    args.putString("searchName", searchName);
        TripFragment fragment = new TripFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_fragment_list, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();
        initNormal();

        if (searchId == 3) {//首页搜索
            httpTripSearch(0);
        } else {
            httpTriplist(0);
        }

        return view;
    }

    @Override
    public void initNormal() {
        datalist = new ArrayList<>();

        initRV();

        norSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(1500);
                datalist.clear();
                if (searchId == 3) {
                    httpTripSearch(0);
                } else {
                    httpTriplist(0);
                }


            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1500);
                if (searchId == 3) {
                    httpTripSearch(pageInt);
                } else {
                    httpTriplist(pageInt);
                }

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    private void initRV() {
        tripAdapter = new TripAdapter(mContext, datalist, nodataTv);
        normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        normalRv.setAdapter(tripAdapter);
        tripAdapter.setOnOneClick(new TripAdapter.OnOneClick() {
            @Override
            public void oneClick(int pos) {
                TripBean tripBean = datalist.get(pos);
                Intent intentde = new Intent(mContext, TripDetailActivity.class);
                intentde.putExtra("tId", tripBean.getId());
                startActivityForResult(intentde, 0x871);
            }
        });

    }

    private void httpTriplist(int myPage) {
        HttpParams params = new HttpParams();
        params.put("times", cTime);
        params.put("page", myPage);
        params.put("start_place", startplace);
        params.put("destination", endplace);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.TRIP_LIST, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                List<TripBean> data = new Gson().fromJson(o, new TypeToken<List<TripBean>>() {
                }.getType());
                pageInt = myPage + 1;
                datalist.addAll(data);
                tripAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    private void httpTripSearch(int myPage) {
        HttpParams params = new HttpParams();
        params.put("type", searchId);
        params.put("search_name", ((SearchHomeActivity) getActivity()).getSearchStr());
        params.put("page", myPage);
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.HOME_SEARCH, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                List<TripBean> data = new Gson().fromJson(o, new TypeToken<List<TripBean>>() {
                }.getType());
                pageInt = myPage + 1;
                datalist.addAll(data);
                tripAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x871://pay success
                getActivity().setResult(0x871);
                getActivity().finish();
                break;
        }
    }
}
